package com.weirdor.wxchat.facade.base.serviceIp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weirdor.wxchat.facade.model.ServiceIP;
import com.weirdor.wxchat.support.exception.WexinReqException;
import com.xiaoleilu.hutool.http.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weirdor on 2017/11/28.
 */
public class ServiceIpAPI {


    /**
     * 返回的信息名称
     */
    public static String RETURN_INFO_NAME = "ip_list";

    public final static String getcallbackip_url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";


    /**
     * 获取服务的ip列表信息
     * @param accessToke
     * @return
     * @throws WexinReqException
     */
    public static List<String> getServiceIpList(String accessToke) throws WexinReqException {
        Map<String,Object> map=new HashMap<>();
        map.put("ACCESS_TOKEN",accessToke);
        String reponse= HttpUtil.get(getcallbackip_url,map);
        JSONObject result =JSONObject.parseObject(reponse);
        List<String> lstServiceIp = null;
        JSONArray infoArray = result.getJSONArray(RETURN_INFO_NAME);
        lstServiceIp = new ArrayList<String>(infoArray.size());
        for (int i = 0; i < infoArray.size(); i++) {
            lstServiceIp.add(infoArray.getString(i));
        }
        return lstServiceIp;
    }


    public static void main(String[] args){

        try {
            List<String> s = ServiceIpAPI.getServiceIpList("gq9gIfOS3rkgDLH965rWV2hpMIQj0PXixyB9Hze8Hi13w38b8geWOFMsRg_TeOxlNmj-XFA-U_I1F_SRbVXDcoZdvpvuH9_beKIsqO4NP-S8VsqCN8G2TKL5ufjQdmzeZITjAHATCN");
            System.out.println(s);
        } catch (WexinReqException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
